//
//  BlackListController.m
//  KingSelected
//
//  Created by 杨鹏 on 2023/11/13.
//

#import "BlackListController.h"
#import "BlackTableViewCell.h"

@interface BlackListController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableview;
@property (nonatomic, strong) NSMutableArray *modelArr;

@end

@implementation BlackListController

- (void)initDidLoad {
    self.navigationTitle = @"黑名单";
}

- (void)initViews {
    [self.view addSubview:self.tableview];
    [self.tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
}

- (void)initRequestData {
    [YPProgressHud showHudInview:self.view];
    [NET getBlackListSuccess:^(id  _Nonnull responseObject) {
        [YPProgressHud hideHudForView:self.view];
        self.modelArr = [UserModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        [self.tableview reloadData];
        [self.tableview.mj_header endRefreshing];
    } failure:^(NSError * _Nonnull error) {
        [YPProgressHud hideHudForView:self.view];
        [self.tableview.mj_header endRefreshing];
    }];
}

- (void)cancelBlackWithUserId:(NSString *)userId {
    [YPProgressHud showHudInview:self.view];
    [NET blackUser:userId type:0 success:^(id  _Nonnull responseObject) {
        [YPProgressHud hideHudForView:self.view];
        [self initRequestData];
    } failure:^(NSError * _Nonnull error) {
        [YPProgressHud hideHudForView:self.view];
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.modelArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BlackTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BlackTableViewCell"];
    if (!cell) {
        cell = [[BlackTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"BlackTableViewCell"];
    }
    cell.model = self.modelArr[indexPath.row];
    @weakify(self)
    [cell setCancelBlock:^(NSString * _Nonnull userId) {
     @strongify(self)
        [self cancelBlackWithUserId:userId];
    }];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 70;
}

#pragma mark - 懒加载
- (UITableView *)tableview {
    if (!_tableview) {
        _tableview = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableview.delegate = self;
        _tableview.dataSource = self;
        _tableview.backgroundColor = [UIColor whiteColor];
        _tableview.showsVerticalScrollIndicator = NO;
        _tableview.showsHorizontalScrollIndicator = NO;
        _tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            [self initRequestData];
        }];
    }
    return _tableview;
}

- (NSMutableArray *)modelArr {
    if (!_modelArr) {
        _modelArr = [NSMutableArray array];
    }
    return _modelArr;
}

@end
